package com.taotao.service.ipml;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.common.pojo.EUDataGridResult;
import com.taotao.common.pojo.Item;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.utils.HttpClientUtil;
import com.taotao.common.utils.IDUtils;
import com.taotao.common.utils.JsonUtils;
import com.taotao.mapper.TbItemMapper;
import com.taotao.pojo.TbItem;
import com.taotao.pojo.TbItemCat;
import com.taotao.pojo.TbItemDesc;
import com.taotao.pojo.TbItemExample;
import com.taotao.pojo.TbItemParamItem;
import com.taotao.pojo.TbItemExample.Criteria;
import com.taotao.service.IItemCatService;
import com.taotao.service.IItemDescService;
import com.taotao.service.IItemParamItemService;
import com.taotao.service.IItemService;

/**
 * 商品的业务层
 * 
 * @author Administrator
 *
 */
@Service
public class ItemServiceIpml implements IItemService {
	@Autowired
	private TbItemMapper tbItemMapper;

	@Autowired
	private IItemDescService itemDescService;

	@Autowired
	private IItemParamItemService itemParamItemService;

	@Autowired
	private IItemCatService iItemCatService;

	@Value("${SOLR_BASE_URL}")
	private String SOLR_BASE_URL;

	@Value("${SOLR_SYNC_ITEM_BASE_INFO_URI}")
	private String SOLR_SYNC_ITEM_BASE_INFO_URI;

	/**
	 * 通过商品id查询商品
	 */
	@Override
	public TbItem getItemById(Long id) {
		// TODO Auto-generated method stub
		TbItemExample example = new TbItemExample();
		Criteria criteria = example.createCriteria();
		criteria.andIdEqualTo(id);
		List<TbItem> tbItems = tbItemMapper.selectByExample(example);
		if (tbItems != null && tbItems.size() > 0) {
			return tbItems.get(0);
		}
		return null;
	}

	/**
	 * 分页获取商品信息
	 */
	@Override
	public EUDataGridResult getItemList(Integer page, Integer rows) {
		// TODO Auto-generated method stub
		EUDataGridResult euge = new EUDataGridResult();
		TbItemExample example = new TbItemExample();
		// 进行分页
		PageHelper.startPage(page, rows);
		List<TbItem> tbItems = this.tbItemMapper.selectByExample(example);
		PageInfo<TbItem> pageInfo = new PageInfo<TbItem>(tbItems);
		euge.setRows(tbItems);
		// 获取总页数
		long total = pageInfo.getTotal();
		euge.setTotal(total);
		return euge;

	}

	/**
	 * 保存商品信息
	 * 
	 * @throws Exception
	 */
	@Override
	public TaotaoResult saveItem(TbItem item, String desc, String itemParams) {
		// TODO Auto-generated method stub
		try {
			// 完善商品信息
			long id = IDUtils.genItemId();
			item.setId(id);
			// '商品状态，1-正常，2-下架，3-删除',
			item.setStatus((byte) 1);
			// 上架时间
			Date date = new Date();
			item.setCreated(date);
			// 更新时间
			item.setUpdated(date);
			// int i = 1 / 0;
			// 插入商品信息
			this.tbItemMapper.insert(item);

			// 插入商品描述信息
			Long itemId = item.getId();
			TbItemDesc itemDesc = new TbItemDesc();
			itemDesc.setItemId(itemId);
			itemDesc.setCreated(date);
			itemDesc.setUpdated(date);
			itemDesc.setItemDesc(desc);
			this.itemDescService.saveItemDesc(itemDesc);

			// 插入规格信息
			TbItemParamItem itemParamItem = new TbItemParamItem();
			itemParamItem.setItemId(itemId);
			itemParamItem.setParamData(itemParams);
			itemParamItem.setCreated(date);
			itemParamItem.setUpdated(date);
			this.itemParamItemService.saveItemParamItem(itemParamItem);

			try {
				// 获取商品种类
				Long itemCatId = item.getCid();
				TbItemCat itemCat = this.iItemCatService
						.getItemByItemCatId(itemCatId);
				// 将商品信息添加到索引库
				Item _item = new Item();
				_item.setId(item.getId() + "");
				_item.setCategory_name(itemCat.getName());
				_item.setImage(item.getImage());
				_item.setItem_desc(desc);
				_item.setSell_point(item.getSellPoint());
				_item.setTitle(item.getTitle());
				_item.setPrice(item.getPrice());
				String value = JsonUtils.objectToJson(_item);
				System.out.println(value);
				HttpClientUtil.doPostJson(SOLR_BASE_URL
						+ SOLR_SYNC_ITEM_BASE_INFO_URI, value);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				throw new RuntimeException("向索引库中插入数据失败");
			}
			return TaotaoResult.ok();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			e.getMessage();
			throw new RuntimeException("添加商品信息发生异常");
		}
	}
}
